
#include <LPC214X.H>
#include <string.h>

#include "efs.h"
#include "ls.h"
//#include "uart.h"


int main(void) {
	
	EmbeddedFileSystem efs;
	EmbeddedFile filer;
	EmbeddedFile filew;
	DirList dList;

	unsigned char buf[513];
	int errorCode = 0;
	errorCode = efs_init(&efs, "\\");
	if(errorCode != 0) 
	{
		//uart1puts("\r\nFile sytem initializing failed\r\n");
		errorCode = 0;
	}
	else
	{
	 	//rprintf("ok\n");
		//rprintf("Directory of 'root':\n");
		ls_openDir(&dList, &(efs.myFs) , "/");
		while ( ls_getNext( &dList ) == 0 ) {
			dList.currentEntry.FileName[LIST_MAXLENFILENAME-1] = '\0';
			/*rprintf( "%s ( %li bytes )\n" ,
				list.currentEntry.FileName,
				list.currentEntry.FileSize ) ; */
		}
	}

	if ( file_fopen( &filer, &efs.myFs , "file.txt" , 'r' ) == 0 ) 
	{
			//rprintf("File %s open. Content:\n", LogFileName);
			while ( ( errorCode = file_read( &filer, 512, buf ) ) != 0 ) 
			{
				//buf[e]='\0';
				//uart1Puts((char*)buf);
			}
			//rprintf("\n");
			file_fclose( &filer );
		}
		
		if ( file_fopen( &filew, &efs.myFs , "file.txt" , 'a' ) == 0 ) 
		{
			//rprintf("File %s open for append. Appending...", LogFileName);
			strcpy((char*)buf, "Martin hat's angehaengt\r\n");
			if ( file_write( &filew, strlen((char*)buf), buf ) == strlen((char*)buf) ) 
			{
				//rprintf("ok\n");
			}
			else 
			{
				//rprintf("fail\n");
			}
			file_fclose( &filew );
		}
		
		if ( file_fopen( &filer, &efs.myFs , "file.txt" , 'r' ) == 0 ) 
		{
			//rprintf("File %s open. Content:\n", LogFileName);
			while ( ( errorCode = file_read( &filer, 512, buf ) ) != 0 ) 
			{
				//buf[errorCode]='\0';
				//uart1Puts((char*)buf);
			}
			//rprintf("\n");
			file_fclose( &filer );
		}
		
		fs_umount( &efs.myFs ) ;

		while(1)
		{

		}

// 	return 0;
}
